﻿Imports MysteryControls
Imports Mystery

Public Class FileContentIconView
    Implements IPropertyControl


    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentReference

    Private _file_content As FileContent

    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentReference)()
            _label = _content_property.label
            _property_info = value
        End Set
    End Property

    Public Property value As Object Implements IPropertyControl.value
        Get
            Return _file_content
        End Get
        Set(value As Object)
            If TypeOf (value) Is SingleContentReference(Of FileContent) Then value = DirectCast(value, ContentPropertyValue).getValue
            Dim new_value As FileContent = value

            myButton.Visibility = If(new_value Is Nothing, Windows.Visibility.Hidden, Windows.Visibility.Visible)
            lNotGiven.Visibility = If(new_value Is Nothing, Windows.Visibility.Visible, Windows.Visibility.Hidden)

            If new_value Is Nothing AndAlso _file_content Is Nothing Then Exit Property
            If new_value IsNot Nothing AndAlso new_value.Equals(_file_content) Then Exit Property
            _file_content = new_value
            Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
            'interfance


            RaiseEvent valueChanged()
        End Set
    End Property


    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property

    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return New GridLength(100)
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Return New List(Of IContent)
    End Function

    Public Event valueChanged() Implements IPropertyControl.valueChanged

    Private Sub myButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles myButton.Click
        If _file_content Is Nothing Then Exit Sub 'nothing to do I don't even know who you managed..

        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        Dim info As System.IO.FileInfo = cd.getFile(_file_content)
        If Not info.Exists Then
            MsgBox("file not found, is your app well configured?")
            Exit Sub
        End If
        Dim argument As String = "/select, " + info.FullName
        Process.Start("explorer.exe", argument)
    End Sub

    
End Class
